﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Guru.Data;
using Guru.Utilities;
public partial class RM_Invoice : HSMPage
{
    protected int InvoiceID
    {
        get
        {
            return int.Parse(SecureQueryString["InvoiceID"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PageType = GuruPageType.Modal;
        this.FixedModalWidth = 720;
        if (!IsPostBack)
        {
            this.LoadByGuest(this.InvoiceID);
            this.LoadDetails();
        }
    }
    public void LoadDetails()
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
        DataTable tb = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetDetails", parameters);

        this.uxInvoiceNumber.Text = tb.Rows[0]["InvoiceNumber"].ValidateResponse();
        this.uxCheckedInDate.Text = tb.Rows[0]["CheckInDate"].FormatDateTime();
        this.uxCheckedOutDate.Text = tb.Rows[0]["CheckOutDate"].FormatDateTime();

        this.uxServiceFees.Text = tb.Rows[0]["TotalServiceFees"].FormatCurrency();
        this.uxRoomFees.Text = tb.Rows[0]["TotalRoomFees"].FormatCurrency();
        this.uxOtherFees.Text = tb.Rows[0]["AddSubTotal"].FormatCurrency();
        this.uxPromotion.Text = tb.Rows[0]["Promotion"].FormatCurrency();
        this.uxVAT.Text = tb.Rows[0]["VAT"].FormatCurrency();

        double amount = tb.Rows[0]["TotalFees"].ToMoney() + tb.Rows[0]["AddSubTotal"].ToMoney() - tb.Rows[0]["Promotion"].ToMoney();
        amount = amount + amount * tb.Rows[0]["VAT"].ToMoney();

        double remaining = amount - tb.Rows[0]["Prepaid"].ToMoney();
        this.uxAmount.Text = amount.FormatCurrency();
        this.uxPrepaid.Text = tb.Rows[0]["Prepaid"].FormatCurrency();
        this.uxRemaining.Text = remaining.FormatCurrency();

        this.uxDescriptions.Text = tb.Rows[0]["Description"].ValidateResponse();
        this.uxCreatedBy.Text = tb.Rows[0]["CreatedByName"].ValidateResponse();
    }
    public void LoadByGuest(int invoiceID)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", invoiceID, DbType.Int32);
        DataTable tbGuest = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Guest_GetByInvoice", parameters);

        this.uxCustomer.Text = tbGuest.Rows[0]["GuestName"].ValidateResponse();
        this.uxCustomerCode.Text = tbGuest.Rows[0]["GuestCode"].ValidateResponse();
        this.uxSignCustomer.Text = tbGuest.Rows[0]["GuestName"].ValidateResponse();
        this.uxAddress.Text = tbGuest.Rows[0]["Address"].ValidateResponse();
    }

    protected void uxReceivedMoney_CheckedChanged(object sender, EventArgs e)
    {
        if (uxReceivedMoney.Checked)
        {
            GuruParameterCollection parameters = new GuruParameterCollection();
            parameters.AddUserLoggedParams();
            parameters.Add("@InvoiceID", SecureQueryString["InvoiceID"], DbType.Int32);
            parameters.Add("@Received", uxReceivedMoney.Checked, DbType.Boolean);
            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_ReceivedMoney", parameters);
        }
    }    
    protected void uxBack_Click(object sender, EventArgs e)
    {
       string qString = string.Format("InvoiceID={0}&RoomID={1}", SecureQueryString["InvoiceID"], SecureQueryString["RoomID"]);
        Response.Redirect("CheckOutModal.aspx?" + BuildSecureQueryString(qString));   
    }
    
}
